<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户注册</title>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600&display=swap" rel="stylesheet">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Poppins', sans-serif;
        }

        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }

        .container {
            background-color: white;
            border-radius: 15px;
            box-shadow: 0 15px 30px rgba(0, 0, 0, 0.2);
            width: 100%;
            max-width: 500px;
            padding: 40px;
            animation: fadeIn 0.5s ease-in-out;
        }

        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(20px); }
            to { opacity: 1; transform: translateY(0); }
        }

        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
            font-weight: 600;
        }

        .form-group {
            margin-bottom: 20px;
            position: relative;
        }

        label {
            display: block;
            margin-bottom: 8px;
            color: #555;
            font-weight: 500;
        }

        input {
            width: 100%;
            padding: 12px 15px;
            border: 2px solid #e0e0e0;
            border-radius: 8px;
            font-size: 16px;
            transition: all 0.3s;
        }

        input:focus {
            border-color: #667eea;
            outline: none;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.2);
        }

        button {
            width: 100%;
            padding: 14px;
            background: linear-gradient(to right, #667eea, #764ba2);
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s;
            margin-top: 10px;
        }

        button:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
        }

        .login-link {
            text-align: center;
            margin-top: 20px;
            color: #666;
        }

        .login-link a {
            color: #667eea;
            text-decoration: none;
            font-weight: 500;
        }

        .error-message {
            color: #e74c3c;
            font-size: 14px;
            margin-top: 5px;
            display: none;
        }

        .success-message {
            color: #2ecc71;
            text-align: center;
            margin-top: 15px;
            display: none;
        }
    </style>
</head>
<body>
<div class="container">
    <h1>创建您的账户</h1>

    <form id="registerForm">
        <div class="form-group">
            <label for="username">用户名</label>
            <input type="text" id="username" name="username" placeholder="请输入用户名" required>
            <div class="error-message" id="username-error"></div>
        </div>

        <div class="form-group">
            <label for="password">密码</label>
            <input type="password" id="password" name="password" placeholder="请输入密码" required>
            <div class="error-message" id="password-error"></div>
        </div>

        <div class="form-group">
            <label for="email">电子邮箱</label>
            <input type="email" id="email" name="email" placeholder="请输入电子邮箱" required>
            <div class="error-message" id="email-error"></div>
        </div>

        <div class="form-group">
            <label for="phone">手机号码</label>
            <input type="tel" id="phone" name="phone" placeholder="请输入手机号码" required>
            <div class="error-message" id="phone-error"></div>
        </div>

        <button type="submit">立即注册</button>

        <div class="success-message" id="success-message"></div>
    </form>

    <div class="login-link">
        已有账号？<a href="/login">立即登录</a>
    </div>
</div>

<script>
    document.getElementById('registerForm').addEventListener('submit', function(e) {
        e.preventDefault();

        // 清除之前的错误信息
        document.querySelectorAll('.error-message').forEach(el => {
            el.style.display = 'none';
        });
        document.getElementById('success-message').style.display = 'none';

        // 获取表单数据
        const formData = {
            username: document.getElementById('username').value,
            password: document.getElementById('password').value,
            email: document.getElementById('email').value,
            phone: document.getElementById('phone').value
        };

        // 简单前端验证
        let isValid = true;

        if (!formData.username) {
            document.getElementById('username-error').textContent = '请输入用户名';
            document.getElementById('username-error').style.display = 'block';
            isValid = false;
        }

        if (!formData.password || formData.password.length < 6) {
            document.getElementById('password-error').textContent = '密码长度至少6位';
            document.getElementById('password-error').style.display = 'block';
            isValid = false;
        }

        if (!formData.email || !/^\S+@\S+\.\S+$/.test(formData.email)) {
            document.getElementById('email-error').textContent = '请输入有效的电子邮箱';
            document.getElementById('email-error').style.display = 'block';
            isValid = false;
        }

        if (!formData.phone || !/^1[3-9]\d{9}$/.test(formData.phone)) {
            document.getElementById('phone-error').textContent = '请输入有效的手机号码';
            document.getElementById('phone-error').style.display = 'block';
            isValid = false;
        }

        if (!isValid) return;

        // 发送注册请求
        fetch('/v1/user/reg', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(formData)
        })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    // 注册成功
                    document.getElementById('success-message').textContent = '注册成功！即将跳转到登录页面...';
                    document.getElementById('success-message').style.display = 'block';
                    document.getElementById('registerForm').reset();

                    // 3秒后跳转到登录页面
                    setTimeout(() => {
                        window.location.href = '/login';
                    }, 3000);
                } else {
                    // 注册失败，显示错误信息
                    if (data.message) {
                        if (data.message.includes('用户名')) {
                            document.getElementById('username-error').textContent = data.message;
                            document.getElementById('username-error').style.display = 'block';
                        } else if (data.message.includes('邮箱')) {
                            document.getElementById('email-error').textContent = data.message;
                            document.getElementById('email-error').style.display = 'block';
                        } else if (data.message.includes('手机')) {
                            document.getElementById('phone-error').textContent = data.message;
                            document.getElementById('phone-error').style.display = 'block';
                        } else {
                            alert(data.message);
                        }
                    }
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('注册失败，请稍后再试');
            });
    });
</script>
</body>
</html>